<template>
  <div class="user">
    <div class="user-t">
      <img src="../assets/img/sisis.jpg" alt="" width="70px" v-show="qh" />
      <img src="../assets/img/mmm.jpg" alt="" width="70px" v-show="!qh" />

      <p v-if="zt">{{ username }}</p>
      <p @click="ifShowModal = true" v-else>点击登录</p>
      <van-icon :name="zt ? 'cross' : 'arrow'" @click="tuichu" />
    </div>
    <!-- 九宫格 -->
    <van-grid square :column-num="3">
      <van-grid-item
        v-for="item in Arr"
        :key="item.id"
        :icon="item.icon"
        :text="item.type"
      />
    </van-grid>
    <!-- 模态框 -->
    <div class="modal" v-show="ifShowModal">
      <div class="modal_bg" @click="ifShowModal = false"></div>
      <div class="modal_content">
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="danger" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Icon } from "vant";
import { Grid, GridItem } from "vant";
import { Form } from "vant";
import { Field } from "vant";
import img1 from "@/assets/img/sisis.jpg";
import img2 from "@/assets/img/mmm.jpg";

Vue.use(Form);
Vue.use(Field);
Vue.use(Grid);
Vue.use(GridItem);
Vue.use(Icon);
import { loginByWeb } from "@/api/denglu";

export default {
  name: "",
  components: { img1, img2 },
  data() {
    return {
      // imgurl: img1,

      // img: [
      //   { id: "1", imgurl: "img1" },
      //   { id: "2", imgurl: "img2" },
      // ],
      qh: true,
      username: "",
      password: "",
      ifShowModal: false,
      zt: false,
      Arr: [
        // grid数组
        { id: 0, icon: "label-o", type: "我的订单" },
        { id: 1, icon: "bill-o", type: "优惠券" },
        { id: 2, icon: "goods-collect-o", type: "礼品卡" },
        { id: 3, icon: "location-o", type: "我的收藏" },
        { id: 4, icon: "flag-o", type: "我的足迹" },
        { id: 5, icon: "contact", type: "会员福利" },
        { id: 6, icon: "aim", type: "地址管理" },
        { id: 7, icon: "warn-o", type: "账号安全" },
        { id: 8, icon: "service-o", type: "联系客服" },
        { id: 9, icon: "question-o", type: "帮助中心" },
        { id: 10, icon: "smile-comment-o", type: "意见反馈" },
      ],
    };
  },
  created() {
    // login();
    let userInfo = localStorage.getItem("userInfo");
    if (userInfo) {
      this.zt = true;
      userInfo = JSON.parse(userInfo);
      this.username = userInfo.username;
      // this.avatarSrc = userInfo.avatar;
      this.qh = false;
    }
  },
  methods: {
    tuichu() {
      if (this.zt) {
        localStorage.removeItem("token");
        localStorage.removeItem("userInfo");
        // 刷新当前页
        // this.imgurl = img1;
        this.qh = true;

        this.$router.go(0);
        alert("确认退出");
      } else {
        this.ifShowModal = true;
      }
    },
    onSubmit(values) {
      console.log(values);
      loginByWeb({
        username: values.用户名,
        pwd: values.密码,
      }).then((res) => {
        console.log(11, res);

        // 存储token
        localStorage.setItem("token", res.data.token);
        // 关闭模态框
        this.ifShowModal = false;
        // 存储userInfo
        let str = JSON.stringify(res.data.userInfo);
        localStorage.setItem("userInfo", str);
        // 修改头像和用户名
        // this.imgurl = img2;

        this.username = res.data.userInfo.username;
        this.zt = true;
        this.qh = false;
      });
    },
    // async login() {
    //   let res = await loginByWeb({
    //     username,
    //     pwd,
    //   });
    //   console.log(11, res);
    // },
  },
};
</script>
<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.user-t {
  height: 80px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  img {
    border-radius: 50%;
  }
}
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: 99;
  left: 0;
  top: 0;
  .modal_bg {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .modal_content {
    width: 90%;
    height: 200px;
    position: absolute;
    background: #fff;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    padding-top: 40px;
  }
}
</style>